package com.leon.ikun.ui.activities.drawerFunction.partition;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;

import androidx.appcompat.widget.AppCompatTextView;

import com.leon.ikun.R;
import com.leon.ikun.base.baseActivity.BaseActivity;
import com.leon.ikun.beans.home.HomeRecommendApp;
import com.leon.ikun.databinding.ActivityPartitionBinding;
import com.leon.ikun.http.BaseUrl;
import com.leon.ikun.http.HttpApi;
import com.leon.ikun.http.RetrofitClient;

import java.util.List;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * @Author Leon
 * @Time 2022/6/21
 * @Desc
 */
public class PartitionActivity extends BaseActivity<ActivityPartitionBinding> {

    private TableLayout tableLayout;


    @Override
    public ActivityPartitionBinding getViewBinding() {
        return ActivityPartitionBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {

        tableLayout = findViewById(R.id.tableLayout);


        // 创建一个数组，包含要显示的图标
        Drawable[] partitionIcons = {
                getResources().getDrawable(R.drawable.partition_douga,null)
        };



        HttpApi httpApi = new RetrofitClient(BaseUrl.JSAPP, context).getHttpApi();

        Observable<HomeRecommendApp> homeRecommendAppObservable = httpApi.jisuClassInfo();

        // 订阅 Observable，并设置观察者
        homeRecommendAppObservable.subscribe(new Observer<HomeRecommendApp>() {

            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull HomeRecommendApp homeRecommendApp) {
                List<HomeRecommendApp.ClassInfo> classList = homeRecommendApp.getClassList();
                String[] array = classList.stream().map(HomeRecommendApp.ClassInfo::getName).toArray(String[]::new);

                // 添加 TextView 到 TableLayout
                runOnUiThread(()->{

                addTextViews(array, partitionIcons);
                });
             }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });




//        binding.douga.setOnClickListener(v -> startPage(1, "动画"));
//        binding.game.setOnClickListener(v -> startPage(4, "游戏"));
//        binding.kichiku.setOnClickListener(v -> startPage(119, "鬼畜"));
//        binding.music.setOnClickListener(v -> startPage(3, "音乐"));
//        binding.dance.setOnClickListener(v -> startPage(129, "舞蹈"));
//        binding.cinephile.setOnClickListener(v -> startPage(181, "影视"));
//        binding.ent.setOnClickListener(v -> startPage(5, "娱乐"));
//        binding.knowledge.setOnClickListener(v -> startPage(36, "知识"));
//        binding.tech.setOnClickListener(v -> startPage(188, "科技"));
//        binding.information.setOnClickListener(v -> startPage(202, "资讯"));
//        binding.food.setOnClickListener(v -> startPage(211, "美食"));
//        binding.life.setOnClickListener(v -> startPage(160, "生活"));
//        binding.car.setOnClickListener(v -> startPage(223, "汽车"));
//        binding.fashion.setOnClickListener(v -> startPage(155, "时尚"));
//        binding.sports.setOnClickListener(v -> startPage(234, "运动"));
//        binding.animal.setOnClickListener(v -> startPage(217, "动物圈"));
    }

    private void addTextViews(String[] titles, Drawable[] icons) {
        // 设置每行显示的 TextView 数量
        final int NUM_COLUMNS = 4;

        for (int i = 0; i < titles.length; i += NUM_COLUMNS) {
            // 创建一个新的 TableRow
            TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT
            ));

            // 添加每行的 TextView
            for (int j = i; j < i + NUM_COLUMNS && j < titles.length; j++) {
                // 创建一个新的 TextView
                AppCompatTextView textView = new AppCompatTextView(this);
                textView.setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT,
                        1f
                ));
                textView.setText(titles[j]);
                textView.setTextColor(getResources().getColor(R.color.primaryContrary));
                textView.setCompoundDrawablesWithIntrinsicBounds(
                        null,
                        icons[0],
                        null,
                        null
                );
                textView.setGravity(Gravity.LEFT);

                // 添加 TextView 到 TableRow
                tableRow.addView(textView);
            }

            // 添加 TableRow 到 TableLayout
            tableLayout.addView(tableRow);
        }
    }

    private void startPage(int tid, String partitionTitle) {
        Bundle bundle = new Bundle();
        bundle.putInt(PartitionDetailActivity.PARAM_A, tid);
        bundle.putString(PartitionDetailActivity.PARAM_B, partitionTitle);

        startActivity(PartitionDetailActivity.class, bundle);
    }
}